package com.example.alibabavodossservice.vod.Image;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.vod.model.v20170321.*;
import com.example.alibabavodossservice.CheckTools;
import org.springframework.beans.BeanUtils;

import java.util.List;

import static com.example.alibabavodossservice.vod.InitVODConstants.VOD_CLIENT;


/**
 * desc: 处理图片指令集
 * @author xugenyin
 */
public class Cmd {

    /**
     * 获取图片上传凭证
     *
     * @param imageParameter 主要请求参数
     * @return 获取图片上传凭证请求响应
     * @throws ClientException 连接异常
     */
    public static CreateUploadImageResponse createUploadImage(ImageParameter imageParameter) throws ClientException {
        ImageParameter.checkMustParam(imageParameter);
        CreateUploadImageRequest uploadImageRequest = new CreateUploadImageRequest();
        uploadImageRequest.setAcceptFormat(FormatType.JSON);
        //设置请求超时时间
        if (!CheckTools.isNullOrEmpty(imageParameter.getSysReadTimeout())) {
            uploadImageRequest.setSysReadTimeout(1000);
        }
        if (!CheckTools.isNullOrEmpty(imageParameter.getSysConnectTimeout())) {
            uploadImageRequest.setSysConnectTimeout(1000);
        }
        BeanUtils.copyProperties(uploadImageRequest, imageParameter);
        return VOD_CLIENT.getAcsResponse(uploadImageRequest);
    }

    /**
     * 根据图片id获取图片信息
     *
     * @param imageId 图片id
     * @return GetImageInfoResponse.ImageInfo
     * @throws ClientException
     */
    public static GetImageInfoResponse.ImageInfo getImageDetailByImageID(String imageId) throws ClientException {
        GetImageInfoRequest getImageInfoRequest = new GetImageInfoRequest();
        getImageInfoRequest.setImageId(imageId);
        GetImageInfoResponse getImageInfoResponse = VOD_CLIENT.getAcsResponse(getImageInfoRequest);
        return getImageInfoResponse.getImageInfo();
    }

    /**
     * 根据图片id获取图片信息
     *
     * @param imageIds 图片ids 图片ID。支持传入多个ID，多个图片ID之间请使用半角逗号（,）分隔
     * @return GetImageInfoResponse.ImageInfo, NonExistImageIds
     * @throws ClientException
     */
    public static GetImageInfosResponse getImageDetailByImageIDs(String imageIds) throws ClientException {
        GetImageInfosRequest getImageInfosRequest = new GetImageInfosRequest();
        getImageInfosRequest.setImageIds(imageIds);
        return VOD_CLIENT.getAcsResponse(getImageInfosRequest);

    }

    /**
     * 批量更新图片信息
     *
     * @param imageInfoList 图片信息列表
     * @return UpdateImageInfosResponse 批量更新图片信息响应数据
     * @throws ClientException 连接异常
     */
    public static UpdateImageInfosResponse bathUpdateImageInfo(List<GetImageInfoResponse.ImageInfo> imageInfoList) throws ClientException {
        UpdateImageInfosRequest updateImageInfosRequest = new UpdateImageInfosRequest();
        updateImageInfosRequest.setUpdateContent(JSON.toJSONString(imageInfoList));
        return VOD_CLIENT.getAcsResponse(updateImageInfosRequest);
    }

    /**
     * 批量更新图片信息
     *
     * @param updateContent 图片信息列表 JSON格式字符串，本方法JSON自动转换
     * @return UpdateImageInfosResponse 批量更新图片信息响应数据
     * @throws ClientException 连接异常
     */
    public static UpdateImageInfosResponse bathUpdateImageInfo(String updateContent) throws ClientException {
        UpdateImageInfosRequest updateImageInfosRequest = new UpdateImageInfosRequest();
        updateImageInfosRequest.setUpdateContent(JSON.toJSONString(updateContent));
        return VOD_CLIENT.getAcsResponse(updateImageInfosRequest);
    }

    /**
     * 批量删除图片
     *
     * @param imageIds 图片id 图片ID。支持传入多个ID，多个图片ID之间请使用半角逗号（,）分隔
     * @return DeleteImageResponse 批量删除图片响应数据
     * @throws ClientException 连接异常
     */
    public static DeleteImageResponse bathDeleteImage(String imageIds) throws ClientException {
        DeleteImageRequest deleteImageRequest = new DeleteImageRequest();
        deleteImageRequest.setImageType("ImageId");
        deleteImageRequest.setImageIds(imageIds);
        return VOD_CLIENT.getAcsResponse(deleteImageRequest);
    }

}
